<?php

use App\Models\Schedule;
use App\Models\Inspection\Line;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

/**
 * 巡检数据模型
 *
 * Date: 17/05/2018
 * @author George
 */
class InspectionsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
    	$inspections = [];
    	$lines = Line::query()->where('project_id', 1)->get();
    	$schedule = Schedule::query()->where('company_id', 1)
			->where('project_id', 1)
			->whereDate('date', date('Y-m-d'))
			->first();

    	foreach ($lines as $line) {
    		if ($circles = $line->circles) {
    			foreach ($circles as $circle) {
    				if ($nodes = $circle->nodes) {
    					foreach ($nodes as $node) {
    						$inspections[] = [
    							'company_id' => 1,
    							'project_id' => 1,
    							'schedule_id' => $schedule->id,
    							'line_id' => $line->id,
    							'circle_id' => $circle->id,
    							'node_id' => $node->id,
    							'user_id' => 1,
    							'user_name' => 'George',
    							'node_name' => $node->name,
    							'longitude' => 128.2837342,
    							'latitude' => 28.2838473,
    							'location' => $node->name,
    							'status' => rand(0, 1),
    							'created_at' => date('Y-m-d H:i:s'),
    							'updated_at' => date('Y-m-d H:i:s')
							];
						}
					}
				}
			}
		}

		DB::table('inspections')->insert($inspections);
    }
}
